import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import store from '@/store/report/index'
axios.defaults.timeout = 60000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
//axios.defaults.headers.common['openid'] = store.state.openid||"";
// axios.defaults.headers.common['Authorization'] = UAstore.get('Authorization') || '';
//axios.defaults.headers.common['Authorization'] = localStorage.getItem('Authorization') || '';
//参考问题：https://segmentfault.com/q/1010000008209308/a-1020000008211227
// 这里可以设置统一请求前缀
// axios.defaults.baseURL = 'http://192.168.0.206:9028';

//请求发出拦截器
axios.interceptors.request.use(
    config => {
        //初始化post参数
        if (config.method === 'post' || config.method === 'put') {
            config.data = qs.stringify(config.data);
        }
        //从本地获取
        //var openid = localStorage.getItem('openid');
        config.headers.openid = store.state.openid || "";
        return config;
    },
    error => {
        Message.error({
            message: '参数错误'
        })
        return Promise.reject(error);
    });

//请求响应拦截器
axios.interceptors.response.use(
    res => {
        return res.data;
    },
    error => {
        store.dispatch('updateLoading', false);
        //这里屏蔽掉promise的错误返回值，否则前端axios不做catch处理时控制台会出错！
        return Promise.reject('服务器异常,被全局响应拦截');
    });

export default axios;